#pragma once
//============================================================================
// Copyright (C) 2010-2013 Brett R. Jones
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================
#include <map>

#ifdef ANDROID_PLATFORM
	// android platform does not support open al
	enum EAudioFormat
	{
		eAudioFormatMp3,
		eMaxAudioFormat // must be last
	};
	class AudioBuffer 
	{
	public:
		U32					m_uiID;
		EAudioFormat		m_eFormat;
		irr::core::stringc	m_strSndFile;
	};

#else
	#include <AL/al.h>
	#include <AL/alc.h>

	// OpenAL audio buffer info
	class AudioBuffer 
	{
	public:
		ALuint m_u32ID;
		ALenum m_eFormat;
	};
#endif //ANDROID_PLATFORM


class AudioSource 
{
public:
	//=== vars ===//
	bool			m_bLoaded;
#ifdef ANDROID_PLATFORM
	U32					m_u32ID;
	irr::core::stringc	m_strSndFile;
	BOOL				m_bLoop;
	F32					m_f32MinGain;
	F32					m_f32MaxGain;
	F32					m_f32ReferenceDistance;
	F32					m_f32RollOff;
#else
	ALuint			m_u32ID;
	irr::core::stringc	m_strSndFile;
#endif
	//=== constructor ===//
	AudioSource(	const AudioBuffer * poAudioBuffer, 
					BOOL	bLoop		= false, 
					F32		f32MinGain	= 0.0f, 
					F32		f32MaxGain	= 1.0f, 
					F32		f32ReferenceDistance = 1.0f, 
					F32		f32RollOff	= 1.0f );
	//=== destructor ===//
	~AudioSource();

	//=== methods ===//
	void SetGain( F32 f32Value );
	void SetPosition( F32 f32X, F32 f32Y, F32 f32Z );
	void SetPitch( F32 f32Value );
	void Play( void );


};

class AudioBase 
{
private:
	//=== vars ===//
	BOOL 												m_bEnabled;
	std::map<irr::core::stringc, AudioBuffer>::iterator AudioBuffersIterator;
	std::map<irr::core::stringc, AudioBuffer> 			m_aoAudioBuffers;

public:
	//=== constructor ===//
	AudioBase();
	//=== destructor ===//
	~AudioBase();

	//! startup
	RCODE	Startup( BOOL bEnabled );
	//! shutdown
	void	Shutdown( void );

	//=== properties ===//
	//! return true if sound is enabled
	BOOL				IsEnabled( void )			{ return m_bEnabled; }
	//! set sound enable
	void				SetIsEnabled( BOOL bEnable ){ m_bEnabled = bEnable; }

	//=== methods ===//
	//! load audio buffer from sound file
	RCODE				LoadBuffer( const irr::core::stringc& strFile );
	//! get audio buffer that is loaded from sound file
	const AudioBuffer *	GetBuffer( const irr::core::stringc& strFile);
	//! free all sound buffers
	void				FreeAllBuffers( void );

	//! set position of sound listener
	void				SetListenPosition( F32 f32X, F32 f32Y, F32 f32Z );
	//! set position of sound listener ( irrlicht coords )
	void				SetListenPosition( irr::core::vector3df& v3fPos );
	//! set position of sound listener ( bullet physics coords ) 
	void				SetListenPosition( btVector3& vec3Pos );


	//! set direction listener if facing
	void				SetListenDirection( F32 f32X, F32 f32Y, F32 f32Z );
	//! set direction of sound listener ( irrlicht coords )
	void				SetListenDirection( irr::core::vector3df& v3fDir );
	//! set direction of sound listener ( bullet physics coords ) 
	void				SetListenDirection( btVector3& vec3Dir );

private:
	//! load vorbis ogg file
	RCODE				LoadVorbisFile( irr::core::stringc& strPathAndFile );

};

